<?php

namespace Biz\service\BrandRights;

use Biz\model\brand\BrandPackage;
use Biz\model\brandRights\BrandPackageRightsConfig;

class BrandPackageService
{
    /**
     * 根据条件查询列表
     *
     * @param $where
     *
     * @return array
     */
    public static function getList($where): array
    {
        return BrandPackage::getListByWhere($where);
    }

    /**
     * 获取总数
     *
     * @param $where
     *
     * @return bool|int|mixed|string
     */
    public static function selectCount($where)
    {
        return BrandPackage::count($where);
    }


    /**
     * 获取一条数据
     *
     * @param $id
     *
     * @return bool|int|mixed|string
     */
    public static function getInfo($id)
    {
        return BrandPackage::getById($id);
    }


    /**
     * 审核操作
     *
     * @param array $params
     *
     * @return bool
     */
    public static function audit(array $params): bool
    {
        $where['id'] = $params['id'];
        $data['package_status'] = $params['package_status'];
        $data['admin_id'] = $params['admin_id'];
        BrandPackage::updateData($data, $where);
        return true;
    }


    public static function copy($sourcePackageId, $adminId)
    {
        $packageInfo = self::getInfo($sourcePackageId);
        if (empty($packageInfo)) {
            throw new \Exception('source id not found!');
        }

        unset($packageInfo['id']);
        unset($packageInfo['add_time']);
        unset($packageInfo['edit_time']);
        unset($packageInfo['pack_weight']);

        $packageData = $packageInfo;
        $packageData['admin_id'] = $adminId;

        $targetPackageName = self::getCopyPackageName($packageInfo['package_name']);

        $packageData['package_name'] = $targetPackageName;

        $pack_type = BrandPackage::getNewPackType($packageInfo['type']);
        if (!$pack_type) {
            throw new \Exception('create pack_type fail!');
        }

        $packageData['pack_type'] = $pack_type;

        $packageData['remark'] = '';

        $packageData['package_status'] = BrandPackage::STATUS_OFF;

        $insertResult = BrandPackage::insertData($packageData);

        if (!$insertResult) {
            throw new \Exception('copy brand package fail!');
        }
        $sourcePackageRights
            = BrandPackageRightsConfigService::getOnePackageRightsConfigs($sourcePackageId);

        $packageRightsConfigs = [];

        foreach ($sourcePackageRights as $sourcePackageRight) {
            $rights = $sourcePackageRight;
            unset($rights['id']);
            unset($rights['addtime']);
            unset($rights['edittime']);
            $rights['package_id'] = $insertResult;

            $rights['addtime']
                = $rights['edittime'] = date('Y-m-d H:i:s', time());

            $packageRightsConfigs[] = $rights;
        }

        BrandPackageRightsConfig::batchInsertData($packageRightsConfigs);

        return true;
    }


    public static function getPackageByName($packageName)
    {
        $where['package_name'] = $packageName;
        $packageInfo = BrandPackage::getInfoByWhere($where);
        if ($packageInfo && is_array($packageInfo) && !empty($packageInfo)) {
            return true;
        }
        return false;
    }


    private static function getCopyPackageName($sourcePackageName)
    {
        $targetPackageName = $sourcePackageName . '-副本';
        $index = 0;
        while (self::getPackageByName($targetPackageName)) {
            $index++;
            $targetPackageName = $sourcePackageName . '-副本' . '(' . strval($index)
                . ')';
        }
        return $targetPackageName;
    }

}